package artibella.media.video 
{
	import artibella.media.video.VideoMediaParameters;
	import artibella.media.events.MediaEvent;
	import artibella.media.AMedia;
	import artibella.media.controls.AController;
	import artibella.media.SoundManager;
	import flash.events.Event;
	import flash.media.SoundTransform;
	import flash.net.NetStream;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class VideoMedia extends AMedia
	{
		private var _ns:NetStream;
		
		public function VideoMedia( u:String, ns:NetStream, parameters:VideoMediaParameters ) 
		{
			_url = u;
			_ns = ns;
			
			_vol = parameters.volume;
			_controller = parameters.controller;

			if( _controller != null ) _controller.media = this;
			
			_ns.play( _url );
			_ns.pause();
			
			super();
		}
		
		override protected function _init( ):void 
		{	
			if ( _url == null ) return;
			
			_reglageAudio =  new SoundTransform();
			_reglageAudio.volume = _vol;
			_reglageAudio.pan = 0;
			
			_ns.soundTransform = _reglageAudio;
			
			dispatchEvent( new MediaEvent( MediaEvent.VOLUME, _vol.toString() ) );
		}
		
		// - - -
		
		override public function play():void
		{
			SoundManager.instance.playSound( this );
			
			_ns.resume();
			
			dispatchEvent( new MediaEvent( MediaEvent.PLAY ) );
		}
		
		override public function pause():void
		{
			_position = _ns.time % _duration;
			
			_ns.pause();
			
			dispatchEvent( new MediaEvent( MediaEvent.PAUSE ) );
		}
		
		override public function stop():void
		{
			//SoundManager.instance.stopSound( this );
			
			_position = 0;

			dispatchEvent( new MediaEvent( MediaEvent.STOP ) );
			
			if ( _ns == null ) return;
			_ns.seek( 0 );
			_ns.pause();
		}
		
		override public function seek( p:Number, paused:Boolean = false ):void
		{
			pause();
			
			_position = p ;
			_ns.seek( p );
			
			dispatchEvent( new MediaEvent( MediaEvent.SEEK, _position.toString() ) );
			
			if( !paused ) play();
		}
		
		// - - -
		
		//override public function get volume():Number
		//{
			//return _reglageAudio.volume;
		//}
		
		override public function set volume( v:Number ):void
		{
			_vol = v;
			_reglageAudio.volume = _vol;
			
			if ( _ns != null ) 	_ns.soundTransform = _reglageAudio;
			
			dispatchEvent( new MediaEvent( MediaEvent.VOLUME, _vol.toString() ) );
		}
		
		
		override public function get position():Number
		{
			_position = _ns.time;
			
			return _position;
		}
		
		override public function get duration():Number
		{
			_duration = ( _ns.client as NetStreamClient ).duree;
			 
			return _duration;
		}
		
		//----- IDisposable
		
		override public function dispose():void
		{
			_ns = null;
			
			super.dispose();
		}
		
	}

}